
/*
  @Author: 于辉
  @Time: 2020-11
  @Tittle: nemulist
  @Description: 菜单列表
*/
var dataList ,layindex;
layui.use(['layer','jquery','form'],function(){
    var layer = layui.layer,
        form = layui.form;

    dataList = new Vue({
        el:"#menuList",
        data:{
            menulist:[],
            showIndex:-1,
            icon:''
        },
       methods:{
           showChildred:function(index,showIndex){
              this.showIndex = showIndex;
           },
           eidtInfo:function(firstIndex,secondIndex){
               form.render();
               var data =  jQuery.extend({}, dataList.menulist[firstIndex]); ;
               if(secondIndex != -1){
                   data = dataList.menulist[firstIndex].children[secondIndex];
               }else{
                   $("#parentUID option").removeAttr("disabled");
                   //上一级菜单移除当前菜单
               }
               $("#parentUID option[value='"+data.id+"']").attr("disabled",true);
               $("#icon option[value='"+data.icon+"']").attr("disabled",true);
               data.hidden = !data.hidden;
               console.log(data)
               form.val('userinfo',data );
               layindex = layer.open({
                   type: 1,
                   title: "菜单信息",
                   closeBtn: 1,
                   shadeClose: true,
                   area: ['603px', '630px'],
                   content: $("#detailInfo")
               })
           },
           deleteItem:function(id){
               layer.confirm('是否要删除该菜单？', {
                   btn: ['删除','取消'] //按钮
               }, function(){
                   ajaxSetting({
                       url:getServerPath("DELETEMENU"),
                       type:'get',
                       data:{id:id},
                       success:function (res) {
                           getMenuList();
                           layer.msg("删除成功!",{icon:6});
                       }
                   })
               }, function(){

               });

           }
       }
    })

    //搜索
    layui.$("#btn_search").on("click", function () {
        getMenuList();
    })
    //添加菜单
    $("#btn_add").click(function(){
        form.render();
        form.on('select(changeIcon)', function (data) {
            dataList.icon = data.value;
            // app.updateSex(data.value) 当然你可以在这直接触发其他事件
        });
        form.val('userinfo',{hidden:true} );
        layindex = layer.open({
            type: 1,
            title: "菜单信息",
            closeBtn: 1,
            shadeClose: true,
            area: ['603px', '630px'],
            content: $("#detailInfo")
        })
    })

    getMenuList();
    getPowers();
    getIconfont();

    //获取菜单列表
    function getMenuList (){
        ajaxSetting({
            url:getServerPath("GETMENULIST"),
            type:"get",
            data:{name:$("#search_input").val()},
            success:function(res){
                console.log(res)
                dataList.menulist = res.data;
                $('#parentUID').empty();
                $("#parentUID").append("<option value=\"\">一级菜单</option>");
                for (var i = 0; i < dataList.menulist.length; i++) {
                    $('#parentUID').append(new Option(dataList.menulist[i].name, dataList.menulist[i].id));// 下拉菜单里添加元素
                }
            }
        })
    }

    //获取权限定义数据
    function getPowers(){
        ajaxSetting({
            url:getServerPath("GETPOWERLIST"),
            data:JSON.stringify({name:'浏览',limit:10000}),
            success:function(res){
                console.log(res);
                $('#viewPower').empty();
                $('#viewPower').append(" <option value=\"\">直接选择或搜索选择</option>");
                var  dataSource = res.data.dataSource;
                for (var i = 0; i < dataSource.length; i++) {
                    $('#viewPower').append(new Option(dataSource[i].page+"|"+dataSource[i].name+"|"+dataSource[i].description, dataSource[i].name));// 下拉菜单里添加元素
                }
            }
        })
    }

    //绑定菜单图标icon
    function getIconfont(){
        $.ajax({
            url:'/js/jsonData/iconfont.json',
            type: "get",//请求方式
            dataType: "json",//数据类型可以为 text xml json  script  jsonp
            success:function(res){
                $('#icon').empty();
                $('#icon').append(" <option value=\"\">直接选择或搜索选择</option>");
                var  dataSource = res;
                for (var i = 0; i < dataSource.length; i++) {
                    $('#icon').append(new Option(dataSource[i], dataSource[i]));// 下拉菜单里添加元素
                }
            }
        })
    }


    //详情页相关方法

    $("#btn_cancle").click(function () {
        layer.close(layindex);
    })
    //保存方法
    form.on('submit(btn_save)', function(data) {
        data = data.field;
        data.hidden = !data.hidden;
        data.parentUID = data.parentUID==''?'-1':data.parentUID;
        console.log(data)
        if(data.icon == null || data.icon == ''){
            layer.msg("请选择菜单图标",{icon:5})
            return false;
        }
        console.log(data)
        ajaxSetting({
            url:  data.id==null || data.id == ''? getServerPath("CREATEMENU"):getServerPath("UPDATEMENU"),
            data:JSON.stringify(data),
            success:function(res){
                getMenuList();
                layer.msg("保存成功!",{icon:6});
                setTimeout(function(){
                    layer.close(layindex)
                },200)
            }
        })
        return false;
    });

});

